<?php 
    class AAppView extends CActiveRecord{
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        // goi den bang can ket noi   
        public function tableName() {                  
            return 'c_application_view';
        }

        public function getDataSearch($login,$from_date,$to_date,$userid){
            if($login!=false){
                $str_sql = "";
                $condition="";

                $condition .= !empty($userid) ? $condition.=" AND appId IN (SELECT id FROM c_application_register WHERE type=1 AND userId=" . $userid . " ) " : "";

                if (!empty($from_date)) {                
                    $from_date = strtotime($from_date);
                    $str_sql .= ' AND create_date >= ' . $from_date;
                }
                else
                {
                    $str_sql.=" ";
                }
                $to_date .= " 23:59:59";
                if (!empty($to_date)) {
                    $to_date = strtotime($to_date);
                    $str_sql .= ' AND create_date <= ' . $to_date;
                }
                else{
                    $str_sql.=" "; 
                }


                $sql = "SELECT count(id) as `count`,os_type From c_application_view WHERE 1 " . $condition . "  and os_type !='0'".$str_sql." GROUP BY os_type";
                //echo $sql;die;
                $connect = Yii::app()->db;
                $command = $connect->createCommand($sql);
                $rows = $command->queryAll();

                $data_count = array();
                foreach($rows as $row){
                    $data_count[$row["os_type"]] = $row["count"];
                }

                if(isset($data_count[1])){
                    $j2me=$data_count[1];
                }else{
                    $j2me=0;
                }
                if(isset($data_count[2])){
                    $android=$data_count[2];
                }else{
                    $android=0;
                }
                if(isset($data_count[3])){
                    $ios=$data_count[3];
                }else{
                    $ios=0;
                }
                if(isset($data_count[4])){
                    $blackbery=$data_count[4];
                }else{
                    $blackbery=0;
                }

                $total=$j2me+$android+$ios+$blackbery;
                $j2me= $j2me>0 ? $j2me*100/$total : 0;
                $android= $android>0 ? $android*100/$total : 0;
                $ios= $ios>0 ? $ios*100/$total : 0;
                $blackbery= $blackbery>0 ? $blackbery*100/$total : 0;
                return array($j2me,$android,$ios,$blackbery,$total);
            } else{
                $total=0;
                $j2me=0; 
                $android=0; 
                $ios=0; 
                $blackbery=0; 
                return array($j2me,$android,$ios,$blackbery,$total);
            }
            
        }
    }
?>
